importScripts('./Cartesian3.js')
importScripts('./st.js')
function txtCell2TimeArr(txt) {
    var rows = txt.split('\r\n')
    rows.pop()
    var time = ''
    var times = []
    var oneTime = []
    rows.forEach(item => {
        if (item[4] === '-') { // 时间行
            if (oneTime.length > 0) times.push({ time: time, timeData: oneTime })
            time = item
            oneTime = []
            return
        }
        var rowNumber = item.split(' ')
        var meshIndex = Number(rowNumber[0])
        var sw = Number(rowNumber[1])
        var ss = Number(rowNumber[2])
        var a = Number(rowNumber[3])
        if (meshIndex && sw && ss) {
            oneTime.push([meshIndex, sw, ss, a, Number(rowNumber[4]), Number(rowNumber[5])])
        } else {
            console.log('网格信息错误', rowNumber)
            return false
        }
    })
    times.push({ time: time, timeData: oneTime })
    return times
}

// 等同于 this.addEventListener / addEventListener
this.addEventListener('message', function(res) {
    const { planID, resCell, resPoint, resMesh, timesNodes, hDiff } = res.data
    var timesCells = txtCell2TimeArr(resCell)
    var resArr = []
    timesCells.forEach((timeCell, timeIndex) => {
        var meshsToSave = []
        timeCell.timeData.forEach(oneCell => {
            var meshIndex = Number(oneCell[0]) // 第一列网格索引
            var pointIndexs = resMesh.pointIndexs[meshIndex] // 从mesh.txt里拿到四个点的索引
            // console.log(pointIndex)
            var oneMesh4Plonlat = []
            pointIndexs.forEach(item => {
                var pi = Number(item)
                var lon = resPoint.lonlats[pi][0]
                var lat = resPoint.lonlats[pi][1]
                var h = timesNodes[timeIndex].timeData.get(pi) + hDiff
                if (!h) {
                    console.log(`获取高度失败:方案ID:${planID},时间:${timeCell.time},网格${oneCell[0]},点索引${pi}`)
                    return
                }
                oneMesh4Plonlat.push(lon)
                oneMesh4Plonlat.push(lat)
                oneMesh4Plonlat.push(h)
            })
            if (oneMesh4Plonlat.length !== 12) {
                // console.log(`获取网格失败:方案ID:${this.planID},时间:${timeCell.time},网格:${oneCell[0]}`)
                return
            }
            var angle = Number(oneCell[3])
            angle = Math.round(angle / 15) * 15 + 180
            // eslint-disable-next-line use-isnan
            if (isNaN(angle)) {
                console.log(`获取角度错误:时间为${timeCell.time},网格${oneCell}`)
                return
            }
            // console.log(angle, maxLon, minLon, maxLat, minLat)
            // 获取网格UV
            var st = getSTOut(oneMesh4Plonlat, angle, resPoint.maxLon, resPoint.minLon, resPoint.maxLat, resPoint.minLat)
            // console.log(st)
            var cartesian3 = Cartesian3.fromDegreesArrayHeights(oneMesh4Plonlat)
            var cartesian3s = []
            cartesian3.map((item) => {
                cartesian3s.push(item.x)
                cartesian3s.push(item.y)
                cartesian3s.push(item.z)
            })
            // 水深
            var deep = Number(oneCell[2])
            var colorIndex = 0
            if (deep > 2) {
                colorIndex = 59
            } else if (deep > 1) {
                colorIndex = Math.round(deep * 20 - 20) + 39
            } else if (deep > 0.5) {
                colorIndex = Math.round((deep - 0.5) * 0.4 * 100) + 20
            } else {
                colorIndex = Math.round(deep * 0.4 * 100)
            }
            var oneMeshToSave = {
                cartesian3s,
                st,
                meshCode: meshIndex,
                sw: Number(oneCell[1]),
                deep: deep,
                a: angle,
                x: oneCell[4],
                y: oneCell[5],
                colorIndex: colorIndex
            }
            meshsToSave.push(oneMeshToSave)
        })
        resArr.push({ time: timeCell.time + '-Out', meshs: meshsToSave })
    })
    this.postMessage(resArr)
    this.close()
}, false)
// 监听错误
this.addEventListener('error', function(e) {
    console.error(e)
}, false)
// this.close(); // 终止通信
